//
//  WTRegisterViewController.m
//  OneDay
//
//  Created by lanou on 16/1/19.
//  Copyright © 2016年 小明. All rights reserved.
//

#import "WTRegisterViewController.h"
#import "WTRegisterView.h"
#import "WTRequestManager.h"

@interface WTRegisterViewController ()

@property (nonatomic, strong) WTRegisterView * registerView;

@property (nonatomic, strong) NSTimer * timer;

// 计数
@property (nonatomic, assign) NSInteger currentTime;
@end

@implementation WTRegisterViewController
-(NSString*) uuid {
    CFUUIDRef puuid = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString( nil, puuid );
    NSString * result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    CFRelease(puuid);
    CFRelease(uuidString);
    return result;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    _registerView = [[WTRegisterView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    [self.view addSubview:_registerView];
    
    [self addRegisterView];
    // Do any additional setup after loading the view.
}

- (void)addRegisterView{
  
    _registerView.userImageV.image = [UIImage imageNamed:@"m-yonghu"];
    _registerView.userTF.placeholder = @"用户名(手机号)";
    _registerView.passwordImageV.image = [UIImage imageNamed:@"m-mima1"];
    _registerView.passwordTF.placeholder = @"密码(6-16位字符,区分大小写)";
    _registerView.repasswordImageV.image = [UIImage imageNamed:@"m-querenmima"];
    _registerView.repasswordTF.placeholder = @"确认密码";
    
    // 验证码
    _registerView.confirmTF.placeholder = @"输入验证码";
    [_registerView.confirmButton setTitle:@"发送验证码" forState:(UIControlStateNormal)];
    [_registerView.confirmButton addTarget:self action:@selector(confirmButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
    [_registerView.registerButton setTitle:@"注册" forState:(UIControlStateNormal)];
    [_registerView.registerButton addTarget:self action:@selector(registerButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
}

- (void)timing:(NSTimer *) timer{
   
    _currentTime--;
    if (_currentTime == 0) {
        [_registerView.confirmButton setTitle:@"获取验证码"forState:(UIControlStateNormal)];
         _registerView.confirmButton.enabled= YES;
        [_timer invalidate];
    }else {
     
      [_registerView.confirmButton setTitle:[NSString stringWithFormat:@"%ld秒后重新获取",(long)_currentTime] forState:(UIControlStateNormal)];
        _registerView.confirmButton.enabled = YES;
    }
}
- (void)confirmButtonClick:(UIButton *)button{
    


    
    
   
    // 发送验证码的reg_phone_number=13645219570&app_id=598835215&os_version=iPhone,8.1.3&action=mier_register&platform=ios&device_uuid=3D6A618C-B635-4F29-871E-5F6C30E1271E&client_id=aa7245b625ca4b3ce01efa7cc09cfd19&user_id=0&t=1&app_version=2.3.1&reg_type=3&controller=register
    
    
    // 获取uuid
    NSUUID *uuid =[UIDevice currentDevice].identifierForVendor;
    
    NSMutableDictionary * parDic = [NSMutableDictionary dictionary];
    parDic[@"reg_phone_number"] = _registerView.userTF.text;
    parDic[@"app_id"] = @"598835215";
    parDic[@"os_version"] = @"iPhone,8.1.3";
    parDic[@"action"] = @"mier_register";
    parDic[@"platform"] = @"ios";
    parDic[@"device_uuid"] = uuid;
    
    parDic[@"client_id"] = @"aa7245b625ca4b3ce01efa7cc09cfd19";
    parDic[@"user_id"] = @"0";
    parDic[@"t"] = @"1";
    parDic[@"app_version"] = @"2.3.1";
    parDic[@"reg_type"] = @"3";
    parDic[@"controller"] = @"register";
    
    
    [WTRequestManager requestWithUrlString:@"http://api.wap.miercn.com/api/apps/index.php" parDic:parDic method:POST success:^(NSData *data) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        
        NSInteger iserror = [dic[@"error"] integerValue];
        // 判断发送成功没有
        if (iserror == 1) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"发送失败" message:dic[@"msg"] preferredStyle:(UIAlertControllerStyleAlert)];
            UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"继续发送" style:(UIAlertActionStyleDefault) handler:nil];
            [alert addAction:okAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else {
            _currentTime = 60;
            _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timing:) userInfo:nil repeats:YES];
        }
        
        
    } error:^(NSError *error) {
        
    }];


}
- (void)registerButtonClick:(UIButton *)sender{
 
    // 获取uuid
    NSUUID *uuid =[UIDevice currentDevice].identifierForVendor;
    
    
    
    NSMutableDictionary * parDic = [NSMutableDictionary dictionary];
    parDic[@"reg_phone_number"] = _registerView.userTF.text;
    parDic[@"app_id"] = @"598835215";
    parDic[@"os_version"] = @"iPhone,8.1.3";
    parDic[@"reg_login_pwd"] = _registerView.passwordTF.text;
    parDic[@"action"] = @"mier_register";
    parDic[@"platform"] = @"ios";
    parDic[@"device_uuid"] = uuid;
    parDic[@"verifycode"] = _registerView.confirmTF.text;
    parDic[@"client_id"] = @"aa7245b625ca4b3ce01efa7cc09cfd19";
    parDic[@"user_id"] = @"0";
    parDic[@"t"] = @"1";
    parDic[@"app_version"] = @"2.3.1";
    parDic[@"reg_type"] = @"3";
    parDic[@"controller"] = @"register";

    
    
    [WTRequestManager requestWithUrlString:@"http://api.wap.miercn.com/api/apps/index.php" parDic:parDic method:POST success:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:dataDic[@"code_desc"] preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
        [alert addAction:okAction];
        [self presentViewController:alert animated:YES completion:nil];
        
        
        if ([dataDic[@"code_desc"] isEqualToString:@"注册成功"]) {
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        
    } error:^(NSError *error) {
        
    }];
    
}



#pragma mark -- 接口:
// http://api.wap.miercn.com/api/2.0.3/get_member.php?plat=iphone&proct=mierapp&versioncode=20150807&apiCode=4&uid=1155619

// 登录
// http://bbs.mier123.com/api/2.0.3/logreg_json.php?plat=android&proct=mierapp&versioncode=20150807&apiCode=4
// act=login&uid=18201916105&pwd=940320

/*
 //  验证码
 http://api.wap.miercn.com/api/apps/index.php
 reg_phone_number=15570753531&app_id=598835215&os_version=iPhone,8.1.3&action=mier_register&platform=ios&device_uuid=3D6A618C-B635-4F29-871E-5F6C30E1271E&client_id=aa7245b625ca4b3ce01efa7cc09cfd19&user_id=0&t=1&app_version=2.3.1&reg_type=3&controller=register
 
 // 注册 http://api.wap.miercn.com/api/apps/index.php
 
 reg_phone_number=15570753531&app_id=598835215&os_version=iPhone,8.1.3&reg_login_pwd=940320&action=mier_register&platform=ios&device_uuid=3D6A618C-B635-4F29-871E-5F6C30E1271E&verifycode=354880&client_id=aa7245b625ca4b3ce01efa7cc09cfd19&user_id=0&t=1&app_version=2.3.1&reg_type=3&controller=register
 
 
 ------------ 验证码
 http://api.wap.miercn.com/api/apps/index.php
 reg_phone_number=13645219570&app_id=598835215&os_version=iPhone,8.1.3&action=mier_register&platform=ios&device_uuid=3D6A618C-B635-4F29-871E-5F6C30E1271E&client_id=aa7245b625ca4b3ce01efa7cc09cfd19&user_id=0&t=1&app_version=2.3.1&reg_type=3&controller=register
 
 
 -- 注册
 reg_phone_number=13645219570&app_id=598835215&os_version=iPhone,8.1.3&reg_login_pwd=123456&action=mier_register&platform=ios&device_uuid=3D6A618C-B635-4F29-871E-5F6C30E1271E&verifycode=140108&client_id=aa7245b625ca4b3ce01efa7cc09cfd19&user_id=0&t=1&app_version=2.3.1&reg_type=3&controller=register
 
 
 // 注册成功后的信息action=mier_acc_detail&app_id=1&app_version=2.3.1&client_id=aa7245b625ca4b3ce01efa7cc09cfd19&controller=user&device_uuid=3D6A618C-B635-4F29-871E-5F6C30E1271E&os_version=iPhone%2C8.1&platform=ios&push_tags=message_center&t=1&user_id=1167088
 
 
 
 */
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
